package tt.com.financialmanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import tt.com.common.model.Dead;
import tt.com.common.model.Order;
import tt.com.common.util.ObjectUtil;
import tt.com.common.util.PageUtil;
import tt.com.financialmanagement.mapper.OrderMapper;
import tt.com.financialmanagement.service.OrderService;
import tt.com.financialmanagement.vo.financeVo;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Override
    public ResponseEntity selectOrder(PageUtil pageUtil) {
        IPage<Order> page = new Page<>(pageUtil.getPageIndex(), pageUtil.getPageSize());
        QueryWrapper<Order> qw=new QueryWrapper<>();
        qw.eq("data_status",1);
        qw.eq("accept_status","未接收");
        IPage<Order> page1 = baseMapper.selectPage(page, qw);
        pageUtil.setData(page1);
        //设计总数
        QueryWrapper<Order> qwe=new QueryWrapper<>();
        qwe.eq("data_status",1);
        qwe.eq("accept_status","未接收");
        pageUtil.setTotalSize(baseMapper.selectCount(qwe));
        //设置总页数
        pageUtil.setTotalPage(pageUtil.getPageSize()/pageUtil.getPageSize());

        return new ResponseEntity(pageUtil, HttpStatus.OK);
    }
//根据项目id进行分类查找
    @Override
    public ResponseEntity selectOrderById(String orderId) {
        PageUtil pageUtil=new PageUtil();
        IPage<Order> page = new Page<>(pageUtil.getPageIndex(), pageUtil.getPageSize());
        QueryWrapper<Order> qw=new QueryWrapper<>();
        qw.eq("data_status",1);
        qw.eq("accept_status","未接收");
        qw.eq("organ_id",orderId);
        IPage<Order> page1 = baseMapper.selectPage(page, qw);
        pageUtil.setData(page1);
        //设计总数
        QueryWrapper<Order> qwe=new QueryWrapper<>();
        qwe.eq("data_status",1);
        qwe.eq("accept_status","未接收");
        qwe.eq("organ_id",orderId);
        pageUtil.setTotalSize(baseMapper.selectCount(qwe));
        //设置总页数
        pageUtil.setTotalPage(pageUtil.getPageSize()/pageUtil.getPageSize());
        return new ResponseEntity(pageUtil, HttpStatus.OK);

    }

    @Override
    public ResponseEntity selectOrderTwo(PageUtil pageUtil) {
        IPage<Order> page = new Page<>(pageUtil.getPageIndex(), pageUtil.getPageSize());
        QueryWrapper<Order> qw=new QueryWrapper<>();
        qw.eq("data_status",1);
        qw.eq("accept_status","接收");
        IPage<Order> page1 = baseMapper.selectPage(page, qw);
        pageUtil.setData(page1);
        //设计总数
        QueryWrapper<Order> qwe=new QueryWrapper<>();
        qwe.eq("data_status",1);
        qwe.eq("accept_status","接收");
        pageUtil.setTotalSize(baseMapper.selectCount(qwe));
        //设置总页数
        pageUtil.setTotalPage(pageUtil.getPageSize()/pageUtil.getPageSize());

        return new ResponseEntity(pageUtil, HttpStatus.OK);

    }

    @Override
    public ResponseEntity delectOrder(Order order) {
        //判断id是否存在
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("id",order.getId());
        List<Order> orders = baseMapper.selectList(wrapper);
        if (orders.size()<0){
            return new ResponseEntity("该信息不存在",HttpStatus.INTERNAL_SERVER_ERROR);
        }

        order.setDataStatus(0);
        order.setDeleteTime(new Date());
        int i = baseMapper.updateById(order);
        if (i<0){
            return new ResponseEntity("删除失败",HttpStatus.INTERNAL_SERVER_ERROR);
        }

        return new ResponseEntity("删除成功",HttpStatus.OK);
    }


    //查询订单已经完成
    @Override
    public ResponseEntity selecFinish(PageUtil pageUtil) {
        IPage<Order> page = new Page<>(pageUtil.getPageIndex(), pageUtil.getPageSize());
        QueryWrapper<Order> qw=new QueryWrapper<>();
        qw.eq("data_status",1);
        qw.eq("status",1);
        IPage<Order> page1 = baseMapper.selectPage(page, qw);
        pageUtil.setData(page1);
        //设计总数
        QueryWrapper<Order> qwe=new QueryWrapper<>();
        qwe.eq("data_status",1);
        qwe.eq("status",1);
        pageUtil.setTotalSize(baseMapper.selectCount(qwe));
        //设置总页数
        pageUtil.setTotalPage(pageUtil.getPageSize()/pageUtil.getPageSize());
        return new ResponseEntity(pageUtil, HttpStatus.OK);


    }

    @Override
    public ResponseEntity backMoney() {
        financeVo financeVo = new financeVo();
        financeVo.setCost(BigDecimal.valueOf(0));
        financeVo.setTotalDiscountAmount(BigDecimal.valueOf(0));
        financeVo.setFruitCost(BigDecimal.valueOf(0));
        //查询完成订单
        QueryWrapper<Order> qw=new QueryWrapper<>();
        qw.eq("data_status",1);
        qw.eq("status",1);
        List<Order> orders = baseMapper.selectList(qw);
        //查询退费的订单
        QueryWrapper<Order> qwe=new QueryWrapper<>();
        qwe.eq("data_status",1);
        qwe.eq("status",2);
        Integer integer = baseMapper.selectCount(qwe);
        //获取退费的总量
        financeVo.setRefundsCont(integer);

        //统计费用
        financeVo.setCount(orders.size());
        for (Order order : orders) {
            //获取总费用
            BigDecimal cost = order.getCost();
            financeVo.setCost(financeVo.getCost().add(cost));
            //获取实际费用
            BigDecimal fruitCost = order.getFruitCost();
            financeVo.setFruitCost(financeVo.getFruitCost().add(fruitCost));
            //获取优惠费用
            BigDecimal totalSubsidyPrice = order.getTotalDiscountAmount();
            financeVo.setTotalDiscountAmount(order.getTotalDiscountAmount().add(totalSubsidyPrice));

        }
        return new ResponseEntity(financeVo,HttpStatus.OK);
    }

    @Override
    public ResponseEntity backMoneyById(String projectId) {

        financeVo financeVo = new financeVo();
        financeVo.setCost(BigDecimal.valueOf(0));
        financeVo.setTotalDiscountAmount(BigDecimal.valueOf(0));
        financeVo.setFruitCost(BigDecimal.valueOf(0));
        PageUtil pageUtil=new PageUtil();
        //查询该项目id已完成订单的信息
        IPage<Order> page = new Page<>(pageUtil.getPageIndex(), pageUtil.getPageSize());
        QueryWrapper<Order> qw=new QueryWrapper<>();
        qw.eq("organ_id",projectId);
        qw.eq("data_status",1);
        qw.eq("status",1);
        IPage<Order> page1 = baseMapper.selectPage(page, qw);
        pageUtil.setData(page1);
        //设计总数
        QueryWrapper<Order> qwe=new QueryWrapper<>();
        qwe.eq("organ_id",projectId);
        qwe.eq("data_status",1);
        qwe.eq("status",1);
        pageUtil.setTotalSize(baseMapper.selectCount(qwe));
        //设置总页数
        pageUtil.setTotalPage(pageUtil.getPageSize()/pageUtil.getPageSize());

        //获取数据
        financeVo.setOrder(pageUtil);
        //获取总条数
        Integer integer=baseMapper.selectCount(qwe);
        financeVo.setCount(integer);

        //查询退费的数量
        QueryWrapper<Order> qwea=new QueryWrapper<>();
        qwea.eq("organ_id",projectId);
        qwea.eq("data_status",1);
        qwea.eq("status",2);
        Integer integer1 = baseMapper.selectCount(qwea);
        financeVo.setRefundsCont(integer1);

        //金额计算
        List<Order> orders = baseMapper.selectList(qw);
        for (Order order : orders) {
            //获取总费用
            BigDecimal cost = order.getCost();
            financeVo.setCost(financeVo.getCost().add(cost));
            //获取实际费用
            BigDecimal fruitCost = order.getFruitCost();
            financeVo.setFruitCost(financeVo.getFruitCost().add(fruitCost));
            //获取优惠费用
            BigDecimal totalSubsidyPrice = order.getTotalDiscountAmount();
            financeVo.setTotalDiscountAmount(financeVo.getTotalDiscountAmount().add(totalSubsidyPrice));

        }


        return new ResponseEntity(financeVo,HttpStatus.OK);
    }


}
